############################################################
# What do people think about government, public, efficacy
# of social distancing, etc.? (these are post-treatment outcomes, 
# though noting that info treatments had precisely null effects).
# 
# Author: 
# Soubhik Barari
# 
# Environment:
# - must use R 3.6
# 
# Input:
# - 00-read_clean_data.R
#
# Output:
# - fig/safety_opinion_x_group{.png,.pdf}
############################################################


source("00-read_clean_data.R")

#----------------------------------------------------------
# summarise
#----------------------------------------------------------

table(responses_df$curfew) %>% prop.table()
#                    No        Yes 
# 0.03128621 0.03765933 0.93105446 

table(responses_df$social) %>% prop.table()
#                    No        Yes 
# 0.03128621 0.02201622 0.94669757 

table(responses_df$handshake) %>% prop.table()
#                    No        Yes 
# 0.03128621 0.01709154 0.95162225 

table(responses_df$stores) %>% prop.table()
#                    No        Yes 
# 0.03128621 0.07850521 0.89020857 

#----------------------------------------------------------
# descriptive plot (safety beliefs)
#----------------------------------------------------------

b_long_df <- responses_df %>%
    tidyr::gather("belief", "value", c("social", "handshake", "stores", "curfew")) %>%
    mutate(belief = 
               ifelse(belief == "social", "Should cancel\nsocial gatherings",
                      ifelse(belief == "handshake", "Should avoid\nhandshakes",
                             ifelse(belief == "stores", "Should close\nnon-essential shops",
                                    ifelse(belief == "curfew", "Should be\ngeneral curfew", "")
                                    )))) %>%
    mutate(value = ifelse(value == "Yes", 1, 0))

library(scales)
b_long_df <- b_long_df %>% 
    group_by(belief) %>%
    summarise(
        y = mean(value)*100
    ) %>% arrange(y)
b_long_df$belief <- factor(b_long_df$belief, levels=b_long_df$belief)
b_long_df %>%
    ggplot(aes(x=belief, y=y)) +
    geom_bar(stat="identity", position = position_dodge(width=0.9)) +
    geom_text(aes(label=sprintf("%0.1f", y), x=belief, y=y+2)) +
    coord_flip() +
    scale_y_continuous(limits=c(80, 100),oob = rescale_none) +
    xlab("") + ylab("percentage of respondents with belief") +
    theme_bw() +
    theme(axis.text.x = element_text(angle=45, size=8))
ggsave("fig/safety_opinion.pdf", width=4, height=2)
ggsave("fig/safety_opinion.png", width=4, height=2)

#----------------------------------------------------------
# descriptive plot (safety beliefs x demographic)
#----------------------------------------------------------

b_long_df <- responses_df %>%
    tidyr::gather("belief", "value", c("social", "handshake", "stores", "curfew")) %>%
    mutate(belief = 
               ifelse(belief == "social", "Should cancel\nsocial\ngatherings",
                      ifelse(belief == "handshake", "Should avoid\nhandshakes",
                             ifelse(belief == "stores", "Should close\nnon-essential\nshops",
                                    ifelse(belief == "curfew", "Should be a\ngeneral\ncurfew", "")
                                    )))) %>%
    mutate(value = ifelse(value == "Yes", 1, 0))

library(scales)
b_long_df %>% 
    filter(gender != "Other") %>%
    tidyr::gather("variable", "variable_value", c("gender", "health", "age_group")) %>%
    select(variable, variable_value, belief, value) %>%
    mutate(variable = 
               ifelse(variable == "age_group", "age group",
                      ifelse(variable == "health", "general health",
                             ifelse(variable == "past_healthy_behavior", "past healthy\nbehavior", "gender"
                             )))) %>%
    mutate(variable_value = factor(variable_value, levels=c(
        "18-29", "30-39", "40-49", "50-59", "60+", "Poor", "Fair", "Good", "Excellent", "Male", "Female", "Other"
    ))) %>%
    group_by(variable, variable_value, belief) %>%
    summarise(
        y = mean(value)*100
    ) %>%
    filter(!is.na(variable_value) & variable_value != "") %>%
    ggplot(aes(x=variable_value, y=y)) +
    geom_text(aes(label=sprintf("%0.1f", y), x=variable_value, y=y+2), size=3) +
    geom_bar(stat="identity", position = position_dodge(width=0.9)) +
    coord_flip() +
    scale_y_continuous(limits=c(85, 103),oob = rescale_none) +
    facet_grid(variable ~ belief, scales = "free", space = "free") +
    xlab("") + ylab("percentage of respondents with belief") +
    theme_bw() +
    theme(axis.text.x = element_text(angle=45, size=8))
ggsave("fig/safety_opinion_x_group.pdf", width=8, height=4)
ggsave("fig/safety_opinion_x_group.png", width=8, height=4)

